<template>
  <div>
    <div>
      <el-button type="primary" @click="publishPage">发表新文章</el-button>
    </div>

    <el-table :data="articles" border style="width: 100%">
      <el-table-column type="index" :index="indexMethod" label="id">
      </el-table-column>
      <el-table-column prop="createTime" label="日期" width="180">
      </el-table-column>
      <el-table-column prop="author" label="作者" width="90">
      </el-table-column>
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="lookNum" sortable label="浏览数" width="85">
      </el-table-column>
      <el-table-column prop="review" sortable label="评论数" width="85">
      </el-table-column>
      <el-table-column prop="likeNum" sortable label="点赞数" width="85">
      </el-table-column>
      <el-table-column prop="tags" label="标签">
        <template slot-scope="scope">
          <el-tag type="primary" disable-transitions v-for="name,value in scope.row.tags">{{name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="'/article/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {
    listAll,
    deleteOne
  } from '@/api/Article.js'

  export default {
    data() {
      return {
        articles: []
      }
    },

    methods: {
      handleDelete(index, row) {
        var that = this
        deleteOne(row.id).then(resp => {
          that.$message({
            type: 'success',
            message: '成功删除'
          })
          that.articles.splice(index, 1)
        }).catch(error => {
          console.log(error)
        })
      },

      indexMethod(index) {
        return index + 1
      },

      publishPage() {
        this.$router.push('/article/edit/0')
      }
    },

    mounted() {
      var that = this
      listAll().then(resp => {
        that.articles = resp.data
      }).catch(error => {
        console.log("error:")
        console.log(error)
      })

    }
  }
</script>

<style>
</style>
